package com.kingwood.algorithm.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @Author 22025812
 * @Description: 手机App防沉迷系统
 * https://fcqian.blog.csdn.net/article/details/135062853
 * @Date: 2024/3/26 13:47
 * @Modified By：
 * @Version 1.0
 */
public class FangChenMi {
    public static class PhoneApp {
        public String name;
        public int weight;
        public int startTime;
        public int endTime;

        public PhoneApp(String name, int weight, int startTime, int endTime) {
            this.name = name;
            this.weight = weight;
            this.startTime = startTime;
            this.endTime = endTime;
        }
    }

    // 这个做法和题意有冲突，测试用例会出错，不纠结跳过
    public static String getResult(String time) {
        String result = "NA";
        // 重新生成列表
//        ArrayList<PhoneApp> registerApps = new ArrayList<>();
//        Arrays.sort(phoneApps, (o1, o2) -> o1.endTime != o2.endTime ? o1.endTime - o2.endTime : o1.weight - o2.weight);
//        int len = phoneApps.length;
//
//        outer:
//        for (int i = len - 1; i >= 0; i--) {
//            PhoneApp app = phoneApps[i];
//            PhoneApp maxWightApp = app;
//            for (int j = i - 1; j >= 0; j--) {
//                PhoneApp preApp = phoneApps[j];
//                // 第一次没交集，直接跳转到外层循环
//                if (preApp.endTime < app.startTime) {
//                    registerApps.add(maxWightApp);
//                    continue outer;
//                } else {
//                    // 找到之前没有交集的第一个app, 直接跳转到外层循环
//                    if (preApp.endTime < app.startTime) {
//                        i = j;
//                        registerApps.add(maxWightApp);
//                        continue outer;
//                    }
//
//                    // 交集内找到权重最大的
//                    if (preApp.weight > maxWightApp.weight) {
//                        maxWightApp = preApp;
//                    }
//                    // 如果到此的话还没有找到和当前app不存在交集的app,那么从i开始到0位置的全都有交集
//                    // 那么把当前最大权重的app加入到列表中，并跳出循环
//                    if (j == 0) {
//                        registerApps.add(maxWightApp);
//                        break outer;
//                    }
//                }
//            }
//
//            if (i == 0) {
//                int size = registerApps.size();
//                if (size <= 0 || (size > 0 && app.endTime < registerApps.get(size - 1).startTime)) {
//                    registerApps.add(app);
//                }
//            }
//        }

        int rtime = covertTime(time);
        // 如果rtime在某个时间段内，则返回该应用的名称
        for (PhoneApp app : phoneAppList) {
            if (app.startTime <= rtime && rtime <= app.endTime) {
                result = app.name;
            }
        }

        return result;
    }

    public static int covertTime(String time) {
        String[] timeArr = time.split(":");
        String hour = timeArr[0];
        String minute = timeArr[1];
        int minutes = Integer.parseInt(hour) * 60 + Integer.parseInt(minute);
        return minutes;
    }

    public static List<PhoneApp> phoneAppList = new ArrayList<>();

    public static void registerApp(PhoneApp phoneApp) {
        int curIndex = 0;
        List<Integer> indexList = new ArrayList<>();
        for (PhoneApp app : phoneAppList) {
            // app 的结束时间要小于当前app的开始时间，才没有交集，否则有交集
            if (!(app.endTime <= phoneApp.startTime)) {
                if (app.weight >= phoneApp.weight) {
                    return;
                }

                indexList.add(curIndex);
            }

            curIndex++;
        }

        if (indexList.size() > 0) {
            indexList.sort((a, b) -> b - a);
            indexList.forEach(index -> phoneAppList.remove((int) index));
        }

        phoneAppList.add(phoneApp);
    }


    public static void main(String[] args) {
        // 2
        //App1 1 09:00 10:00
        //App2 2 09:10 09:30
        //App3 1 09:30 09:50
        //09:20

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        PhoneApp[] phoneApps = new PhoneApp[n];

        for (int i = 0; i < n; i++) {
            String[] itemArr = scanner.nextLine().split(" ");
            int weight = Integer.parseInt(itemArr[1]);
            int startTime = covertTime(itemArr[2]);
            int endTime = covertTime(itemArr[3]);
            PhoneApp phoneApp = new PhoneApp(itemArr[0], weight, startTime, endTime);
            registerApp(phoneApp);
        }

        String time = scanner.next();
        String ans = getResult(time);
        System.out.println(ans);
    }

}
